//
//  WalletLocalData.m
//  MyWallet
//
//  Created by 小肥羊 on 15/7/20.
//  Copyright (c) 2015年 小肥羊. All rights reserved.
//

#import "WalletLocalData.h"


@implementation WalletLocalData

#pragma mark - 单例
+ (WalletLocalData *)sharedManager;
{
    static WalletLocalData *sharedAccountManagerInstance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedAccountManagerInstance = [[self alloc] init];
        [sharedAccountManagerInstance initValue];
    });
    return sharedAccountManagerInstance;
}

- (void)initValue
{
    NSLog(@"%@",DATABASE_PATH);
    self.db = [FMDatabase databaseWithPath:DATABASE_PATH];
    [self.db open];
    //创建纪录表
    NSString *createRecordTable = @"CREATE TABLE IF NOT EXISTS 'RecordTable' ('RecordDtate' DATETIME PRIMARY KEY NOT NULL UNIQUE, 'MoneyFrom' VARCHAR, 'CheckoutType' VARCHAR, 'Money' MONEY, 'MoneyFor' VARCHAR, 'Remarks' VARCHAR)";
    if ([self.db executeUpdate:createRecordTable]) {
        NSLog(@"创建碎片数据表");
    } else {
        NSLog(@"创建碎片数据表失败");
    }
    [self upDataWithMessage:@"创建表"];
    [self.db close];
}

#pragma mark - 开启关闭
+ (BOOL)openDB
{
    if ([[WalletLocalData sharedManager].db open]) {
        NSLog(@"数据库打开");
        [[WalletLocalData sharedManager] upDataWithMessage:@"数据库打开"];
        return YES;
    } else {
        NSLog(@"数据库打开失败");
        return NO;
    }
}
+ (BOOL)closeDB
{
    if ([[WalletLocalData sharedManager].db close]) {
        NSLog(@"数据库关闭");
        return YES;
    } else {
        NSLog(@"数据库关闭失败");
        return NO;
    }
}

#pragma mark - 数据操作
/*! @brief 添加纪录 */
- (BOOL)addRecord:(NSDate *)recordDtate MoneyFrom:(NSString *)moneyFrom CheckoutType:(BOOL)checkoutType Money:(float)money MoneyFor:(NSString *)moneyFor Remarks:(NSString *)remarks
{
    NSString *addRecord = @"INSERT INTO 'RecordTable' ('RecordDtate','MoneyFrom','CheckoutType','Money','MoneyFor','Remarks') VALUES (?,?,?,?,?,?)";
    if ([self.db executeUpdate:addRecord,recordDtate,moneyFrom,@(checkoutType),[NSNumber numberWithFloat:money],moneyFor,remarks]) {
        NSLog(@"添加");
        [self upDataWithMessage:@"添加数据"];
        return YES;
    } else {
        NSLog(@"添加失败");
        return NO;
    }
}


///*! @brief 修改纪录 */
//- (BOOL)modifyUser:(NSString *)sellerID sellerName:(NSString *)sellerName {
//    NSString *modifyUser=@"update SellerUser set SellerName = ? where SellerID = ?";
//    if ([self.db executeUpdate:modifyUser,sellerName,sellerID]) {
//        NSLog(@"修改");
//        return YES;
//    } else {
//        NSLog(@"修改失败");
//        return NO;
//    }
//}

#pragma mark - 查找
- (FMResultSet *)searchAllRecordTable:(NSInteger)count {
    if (count == 0) {
        return [self.db executeQuery:@"select * from RecordTable order by RecordDtate desc"];
    } else {
        return [self.db executeQuery:[NSString stringWithFormat:@"select * from RecordTable order by RecordDtate desc limit %@",[@(count) stringValue]]];
    }
}

- (NSMutableArray *)recordArrayWithCount:(NSInteger)count
{
    NSMutableArray *array = [[NSMutableArray alloc] init];
    FMResultSet *record = [self searchAllRecordTable:count];
    while ([record next]) {
        NSMutableDictionary *recordDict = [[NSMutableDictionary alloc] init];
        [recordDict setObject:[record dateForColumn:@"RecordDtate"] forKey:@"RecordDtate"];
        [recordDict setObject:[record stringForColumn:@"MoneyFrom"] forKey:@"MoneyFrom"];
        [recordDict setObject:[record stringForColumn:@"CheckoutType"] forKey:@"CheckoutType"];
        [recordDict setObject:[NSNumber numberWithFloat:[record longForColumn:@"Money"]] forKey:@"Money"];
        [recordDict setObject:[record stringForColumn:@"MoneyFor"] forKey:@"MoneyFor"];
        [recordDict setObject:[record stringForColumn:@"Remarks"] forKey:@"Remarks"];
        [array addObject:recordDict];
    }
    return array;
}

- (NSMutableArray *)recordArrayWithDay:(NSDate *)day
{
    NSMutableArray *array = [[NSMutableArray alloc] init];
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comps;
    //当前的时分秒获得
    comps =[calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute |NSCalendarUnitSecond) fromDate:day];
    NSInteger hour = [comps hour];
    NSInteger minute = [comps minute];
    NSInteger second = [comps second];
    //零点
    NSTimeInterval timeInterval = day.timeIntervalSince1970 - hour*3600 - minute*60 - second;
    
    FMResultSet *record = [self.db executeQuery:[NSString stringWithFormat:@"select * from RecordTable where RecordDtate > %f and RecordDtate < %f",timeInterval,timeInterval + hour*3600]];
    while ([record next]) {
        NSMutableDictionary *recordDict = [[NSMutableDictionary alloc] init];
        [recordDict setObject:[record dateForColumn:@"RecordDtate"] forKey:@"RecordDtate"];
        [recordDict setObject:[record stringForColumn:@"MoneyFrom"] forKey:@"MoneyFrom"];
        [recordDict setObject:[record stringForColumn:@"CheckoutType"] forKey:@"CheckoutType"];
        [recordDict setObject:[NSNumber numberWithFloat:[record longForColumn:@"Money"]] forKey:@"Money"];
        [recordDict setObject:[record stringForColumn:@"MoneyFor"] forKey:@"MoneyFor"];
        [recordDict setObject:[record stringForColumn:@"Remarks"] forKey:@"Remarks"];
        [array addObject:recordDict];
    }
    return array;
}

#pragma mark - 更新
- (void)upDataWithMessage:(NSString *)message
{
    if (self.delegate) {
        [self.delegate upWalletLocalData:self Message:message];
    }
}



@end
